package com.peach.inquiry.imsdk.tim.conversation;

import com.peach.inquiry.imsdk.tim.message.MsgUtil;
import com.tencent.imsdk.v2.V2TIMConversation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ConversationUtil {


    /**
     * 按照会话 lastMessage 的 timestamp 对 会话列表做排序
     *
     * @return
     */
    public static void sortByLastMsgTimestamp(ArrayList<ConversationBean> oriList) {

        Collections.sort(oriList, new Comparator<ConversationBean>() {
            @Override
            public int compare(ConversationBean o1, ConversationBean o2) {
                if (o1.getLastMsg.timestamp > o2.getLastMsg.timestamp) {
                    return -1;
                } else {
                    return 0;
                }
            }

        });
    }


    /**
     * 腾讯sdk的会话列表对象转换成自定义会话列表对象,
     * 已经按照会话 lastMessage 的 timestamp 对 会话列表做排序
     *
     * @param orgList 腾讯sdk的会话列表对象
     * @return 自定义会话列表对象
     */
    public static ArrayList<ConversationBean> V2TIMConversationList2ConversationBeanList(List<V2TIMConversation> orgList) {
        ArrayList<ConversationBean> conList = new ArrayList<ConversationBean>();
        for (V2TIMConversation org : orgList) {
            conList.add(V2TIMConversation2ConversationBean(org));
        }
        sortByLastMsgTimestamp(conList);
        return conList;
    }


    /**
     * 腾讯sdk的会话对象转换成自定义会话对象
     *
     * @param org 腾讯sdk的会话对象
     * @return 自定义会话对象
     */
    public static ConversationBean V2TIMConversation2ConversationBean(V2TIMConversation org) {
        ConversationBean con = new ConversationBean();

        con.conversationID = org.getConversationID();
        con.type = org.getType();
        con.unreadCount = org.getUnreadCount();
        con.getLastMsg = MsgUtil.V2TIMMessage2MessageBean(org.getLastMessage());

        return con;
    }

}
